var mongodb = require('mongodb');
var BSON = require('mongodb').pure().BSON;
var ObjectID = require('mongodb').ObjectID;
/*
if(process.env.VCAP_SERVICES){
    var env = JSON.parse(process.env.VCAP_SERVICES);
    var mongo = env['mongodb-1.8'][0].credentials;
}
else{
    var mongo = {
            "hostname":"ds039078.mongolab.com",
            "port":39078,
            "username":"admin",
            "password":"mta2013",
            "db":"activiteam"
    };
}

var generate_mongo_url = function(obj){
    obj.hostname = (obj.hostname || 'localhost');
    obj.port = (obj.port || 27017);
    obj.db = (obj.db || 'test');

    if(obj.username && obj.password){
        return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
    }
    else{
        return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
    }
};
*/
var mongourl = process.env.MONGOLAB_DB ? process.env.MONGOLAB_DB : 'mongodb://admin:mta2013@ds039078.mongolab.com:39078/activiteam';//generate_mongo_url(mongo);

var usersIndex = function (){
    mongodb.connect(mongourl, function(err, db) {
        if (err) 
            return false;
        else {
            //create the index for the users collection:
            db.collection('users',function(err,collection){
                if (err)
                    return false;
                collection.ensureIndex({"email":1},{unique:true},function(err,temp){
                    if (err)
                        return false;
                    else
                        return true;
                });
            });
        }
    });
};

var messagesIndex = function (){
    mongodb.connect(mongourl, function(err, db) {
        if (err) 
            return false;
        else {
            //create the index for the users collection:
            db.collection('messages',function(err,collection){
                if (err)
                    return false;
                collection.ensureIndex({"toEmail":1},{unique:false},function(err,temp){
                    if (err)
                        return false;
                    else
                        collection.ensureIndex({"fromEmail":1},{unique:false},function(err,temp){
                            if (err) return false;
                            else return true;
                        });
                });
            });
        }
    });
};

var commentsIndex = function (){
    mongodb.connect(mongourl, function(err, db) {
        if (err) 
            return false;
        else {
            //create the index for the users collection:
            db.collection('comments',function(err,collection){
                if (err)
                    return false;
                collection.ensureIndex({"itemID":1},{unique:false},function(err,temp){
                    if (err)
                        return false;
                    else
                        return true;
                });
            });
        }
    });
};

var teamsIndex = function (){
    mongodb.connect(mongourl, function(err, db) {
        if (err) 
            return false;
        else {
            //create the index for the users collection:
            db.collection('teams',function(err,collection){
                if (err)
                    return false;
                collection.ensureIndex({"members":1},{unique:false},function(err,temp){
                    if (err)
                        return false;
                    else
                        return true;
                });
            });
        }
    });
};

var eventsIndex = function (){
    mongodb.connect(mongourl, function(err, db) {
        if (err) 
            return false;
        else {
            //create the index for the users collection:
            db.collection('events',function(err,collection){
                if (err)
                    return false;
                collection.ensureIndex({"members":1},{unique:false},function(err,temp){
                    if (err)
                        return false;
                    else
                        return true;
                });
            });
        }
    });
};

/// creating all the required indices for the database:
/// INPUT: none
/// OUTPUT: none
exports.setIndices = function(req, res) {
    usersIndex();
    messagesIndex();
    commentsIndex();
    teamsIndex();
    eventsIndex();
    res.send();
    return;
};
